RM.Pages.Functions.Groups_List = {
    CheckSelection : function(){
        if (RM.Pages.Groups_List_Columns_SM.getSelections().length == 0) {
            Ext.MessageBox.alert(
                RM.Translate.Common.Error,
                RM.Translate.Common.NeedToSelectRows
            );
            return false;
        }
        return true;
    },

    DeleteRequest : function(){
        var selections = RM.Pages.Groups_List_Columns_SM.getSelections();
        var parametersJson = [];
        for (var i = 0; i < selections.length; i++){
            parametersJson[i] = {
                name : 'ids[]',
                value : selections[i].data.id
            };
        }
        var myMask = new Ext.LoadMask(
            'rm_pages_groups_list',
            {msg:RM.Translate.Common.PleaseWait}
        );
        myMask.show();
        Ext.Ajax.request({
            url: RM.Common.AssembleURL({
                controller : 'Groups_List',
                action: 'deletejson',
                parameters : parametersJson
            }),
            method: 'POST',
            success: function(responseObject) {
                RM.Pages.Groups_List_Json_Store.load();
                myMask.hide();
            },
            failure: function() {
                Ext.MessageBox.alert(RM.Translate.Common.UnconfirmFailure);
                myMask.hide();
            }
        });
    },

    Delete : function(){
        if (this.CheckSelection() == false) {
            return;
        }
        Ext.MessageBox.confirm(
            RM.Translate.Common.Delete,
            RM.Translate.Admin.Groups.List.DeleteAlert,
            function(buttonID){
                if (buttonID != 'yes') {
                    return;
                }
                RM.Pages.Functions.Groups_List.DeleteRequest();
            }
        );
    },

    Edit : function(){
        if (this.CheckSelection() == false) {
            return;
        }
        var selectedGroupID = RM.Pages.Groups_List_Columns_SM.getSelections()[0].data.id;
        RM.Pages.Functions.Groups_Edit.Request(selectedGroupID, RM.Language);
    },    

    New : function(){
        RM.Pages.Functions.Groups_New_Main();
    }
};

RM.Pages.Groups_List_Toolbar = {
    xtype : "panel",
    id : "rm_pages_groups_list_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"new.gif", label: RM.Translate.Common.New, link: "RM.Pages.Functions.Groups_List.New()"},
        {image: RM.BaseLargeImageURL+"edit.gif", label: RM.Translate.Common.Edit, link: "RM.Pages.Functions.Groups_List.Edit()"},
        {image: RM.BaseLargeImageURL+"delete.gif", label: RM.Translate.Common.Delete, link: "RM.Pages.Functions.Groups_List.Delete()"}
    ])
};
RM.Toolbars.push(RM.Pages.Groups_List_Toolbar);

RM.Pages.Functions.Groups_ListJson = function () {
    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_groups_list_toolbar');
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_groups_list');
    RM.Pages.Groups_List_Json_Store.load();
};

RM.Pages.Groups_List_Json_Store = new Ext.data.JsonStore({
    url: RM.Common.AssembleURL({
        controller: 'Groups_List',
        action: 'listjson'
    }),
    totalProperty: 'total',
    root: 'data',
    fields: [
        {name: "id"},
        {name: "name"},
        {name: "unit_name"},
        {name: "main_unit_id"},
        {name: "published"}
    ],
    sortInfo: {
        field: 'id',
        direction: 'ASC'
    },
    remoteSort: true
});

RM.Pages.Groups_List_Columns_SM = new Ext.grid.CheckboxSelectionModel();
RM.Pages.Groups_List_Columns = new Ext.grid.ColumnModel({    
    defaults: {
        sortable: true
    },
    columns: [
        RM.Pages.Groups_List_Columns_SM,
        {
            dataIndex: 'id',
            header: RM.Translate.Common.Id,
            hidden: true,
            hideable: false
        },{
            dataIndex: 'name',            
            header: RM.Translate.Admin.Groups.List.Name,
            editor: new Ext.form.TextField({
                allowBlank: false
            }),
            hideable: false
        },{
            dataIndex: "main_unit_id",
            header: RM.Translate.Admin.Groups.List.Unit,
            renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                var renderedValue = '--';
                if (record.data.main_unit_id) {
                    renderedValue = record.data.unit_name + " (ID: "+ record.data.main_unit_id + ")";
                }
                return renderedValue;
            }
        },{
            dataIndex: "published",
            header: RM.Translate.Admin.Groups.List.Published,
            renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                if (value == "1") {
                    metaData.css = 'RM_list_enabled_icon';
                } else {
                    metaData.css = 'RM_list_disabled_icon';
                }
            }
        }
    ]
});
RM.Pages.Groups_List_Grid = new Ext.grid.GridPanel({
    clicksToEdit: 1,
    stripeRows: true,
    height: RM.Common.GetPanelHeight(80),
    bbar : new Ext.PagingToolbar({
        store: RM.Pages.Groups_List_Json_Store,
        pageSize: 30
    }),
    remoteSort: true,
    enableColLock : false,
    loadMask : {
        msg: RM.Translate.Common.PleaseWait
    },
    cm : RM.Pages.Groups_List_Columns,
    store : RM.Pages.Groups_List_Json_Store,
    sm : RM.Pages.Groups_List_Columns_SM,
    viewConfig: {
        forceFit: true
    },
    autoExpandColumn: 1
});

RM.Pages.Groups_List = new Ext.Panel({
    id : 'rm_pages_groups_list',
    title : RM.Translate.Common.List,
    iconCls: "RM_list_default_root_icon",
    items : [
        RM.Pages.Groups_List_Grid
    ]
});

RM.Main.Pages.push(RM.Pages.Groups_List);